package api

import (
	"buding-ai/internal/service"
	"buding-ai/internal/web/dto"
	"buding-ai/internal/web/response"
	"buding-ai/pkg/utils/paginate"
	"github.com/gin-gonic/gin"
	"strconv"
)

// McpApi MCP API结构体
type McpApi struct {
	router *gin.RouterGroup
	mcpSrv *service.McpService
	BaseApi
}

// NewMcpApi 创建MCP API实例
func NewMcpApi(router *gin.RouterGroup) *McpApi {
	api := &McpApi{
		router: router,
		mcpSrv: service.NewMcpService(),
	}
	api.router.POST("/add", api.add)
	api.router.DELETE("/delete", api.delete)
	api.router.POST("/update", api.update)
	api.router.GET("/getById", api.getById)
	api.router.GET("/page", api.page)
	return api
}

// add 添加MCP
func (api *McpApi) add(ctx *gin.Context) {
	var mcpDTO dto.McpDTO
	if err := ctx.ShouldBindJSON(&mcpDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}

	mcp, err := api.mcpSrv.Create(mcpDTO)
	if err != nil {
		switch err {
		case service.ErrMcpExists:
			response.ParamError(ctx, "MCP名称已存在")
		case service.ErrMcpConnection:
			response.InternalError(ctx, "连接到MCP失败", err)
		default:
			response.InternalError(ctx, "创建MCP失败", err)
		}
		return
	}

	response.Success(ctx, "添加MCP成功", mcp)
}

// delete 删除MCP
func (api *McpApi) delete(ctx *gin.Context) {
	idStr := ctx.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.ParamError(ctx, "无效的MCP ID")
		return
	}

	err = api.mcpSrv.Delete(id)
	if err != nil {
		switch err {
		case service.ErrMcpNotFound:
			response.NotFound(ctx, "MCP不存在")
		default:
			response.InternalError(ctx, "删除MCP失败", err)
		}
		return
	}

	response.Success(ctx, "删除MCP成功", nil)
}

// update 更新MCP信息
func (api *McpApi) update(ctx *gin.Context) {
	var mcpDTO dto.McpDTO
	idStr := ctx.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.ParamError(ctx, "无效的MCP ID")
		return
	}

	if err := ctx.ShouldBindJSON(&mcpDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}

	err = api.mcpSrv.Update(id, mcpDTO)
	if err != nil {
		switch err {
		case service.ErrMcpNotFound:
			response.NotFound(ctx, "MCP不存在")
		case service.ErrMcpExists:
			response.ParamError(ctx, "MCP名称已被使用")
		default:
			response.InternalError(ctx, "更新MCP信息失败", err)
		}
		return
	}

	response.Success(ctx, "更新MCP信息成功", nil)
}

// getById 根据ID获取MCP信息
func (api *McpApi) getById(ctx *gin.Context) {
	idStr := ctx.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.ParamError(ctx, "无效的MCP ID")
		return
	}

	mcp, err := api.mcpSrv.GetById(id)
	if err != nil {
		switch err {
		case service.ErrMcpNotFound:
			response.NotFound(ctx, "MCP不存在")
		default:
			response.InternalError(ctx, "获取MCP信息失败", err)
		}
		return
	}

	response.Success(ctx, "获取MCP信息成功", mcp)
}

// page 分页获取MCP列表
func (api *McpApi) page(ctx *gin.Context) {
	// 获取分页参数
	pageRequest := paginate.GetPageParams(ctx)

	pageResult, err := api.mcpSrv.GetPage(pageRequest)
	if err != nil {
		response.InternalError(ctx, "获取MCP列表失败", err)
		return
	}
	response.SuccessWithData(ctx, pageResult)
}
